﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract Report typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class ReportDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<ReportDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Report _currentReport = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxReportDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxReportErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxReportBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Naziv property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNazivDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the IDParent property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDParentDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the SelectString property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxSelectStringDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Tip property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxTipDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Kolumne property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxKolumneDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Filter property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxFilterDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the FilterAdv property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxFilterAdvDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Poredak property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxPoredakDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Report> _ReportList;
				
		/// <summary> 
		/// The list of Report to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Report> ReportList
		{
			get {return this._ReportList;}
			set
			{
				this._ReportList = value;
				this.uxReportBindingSource.DataSource = null;
				this.uxReportBindingSource.DataSource = value;
				this.uxReportDataGridView.DataSource = null;
				this.uxReportDataGridView.DataSource = this.uxReportBindingSource;				
				//this.uxReportBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxReportBindingSource_ListChanged);
				this.uxReportBindingSource.CurrentItemChanged += new System.EventHandler(OnReportBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnReportBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentReport = uxReportBindingSource.Current as Entities.Report;
			
			if (_currentReport != null)
			{
				_currentReport.Validate();
			}
			//_Report.Validate();
			OnCurrentEntityChanged();
		}

		//void uxReportBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Report"/> instance.
		/// </summary>
		public Entities.Report SelectedReport
		{
			get {return this._currentReport;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxReportDataGridView.VirtualMode;}
			set
			{
				this.uxReportDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxReportDataGridView.AllowUserToAddRows;}
			set {this.uxReportDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxReportDataGridView.AllowUserToDeleteRows;}
			set {this.uxReportDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxReportDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxReportDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="ReportDataGridViewBase"/> class.
		/// </summary>
		public ReportDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxReportDataGridView = new System.Windows.Forms.DataGridView();
			this.uxReportBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxReportErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNazivDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDParentDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxSelectStringDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxTipDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxKolumneDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxFilterDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxFilterAdvDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxPoredakDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxReportDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxReportBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxReportErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxReportErrorProvider
			// 
			this.uxReportErrorProvider.ContainerControl = this;
			this.uxReportErrorProvider.DataSource = this.uxReportBindingSource;						
			// 
			// uxReportDataGridView
			// 
			this.uxReportDataGridView.AutoGenerateColumns = false;
			this.uxReportDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxReportDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxNazivDataGridViewColumn,
		this.uxIDParentDataGridViewColumn,
		this.uxSelectStringDataGridViewColumn,
		this.uxTipDataGridViewColumn,
		this.uxKolumneDataGridViewColumn,
		this.uxFilterDataGridViewColumn,
		this.uxFilterAdvDataGridViewColumn,
		this.uxPoredakDataGridViewColumn			});
			this.uxReportDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxReportDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxReportDataGridView.Name = "uxReportDataGridView";
			this.uxReportDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxReportDataGridView.TabIndex = 0;	
			this.uxReportDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxReportDataGridView.EnableHeadersVisualStyles = false;
			this.uxReportDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnReportDataGridViewDataError);
			this.uxReportDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnReportDataGridViewCellValueNeeded);
			this.uxReportDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnReportDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxNazivDataGridViewColumn
			//
			this.uxNazivDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNazivDataGridViewColumn.DataPropertyName = "Naziv";
			this.uxNazivDataGridViewColumn.HeaderText = "Naziv";
			this.uxNazivDataGridViewColumn.Name = "uxNazivDataGridViewColumn";
			this.uxNazivDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNazivDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNazivDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDParentDataGridViewColumn
			//
			this.uxIDParentDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDParentDataGridViewColumn.DataPropertyName = "IDParent";
			this.uxIDParentDataGridViewColumn.HeaderText = "IDParent";
			this.uxIDParentDataGridViewColumn.Name = "uxIDParentDataGridViewColumn";
			this.uxIDParentDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDParentDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDParentDataGridViewColumn.ReadOnly = false;		
			//
			// uxSelectStringDataGridViewColumn
			//
			this.uxSelectStringDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxSelectStringDataGridViewColumn.DataPropertyName = "SelectString";
			this.uxSelectStringDataGridViewColumn.HeaderText = "SelectString";
			this.uxSelectStringDataGridViewColumn.Name = "uxSelectStringDataGridViewColumn";
			this.uxSelectStringDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxSelectStringDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxSelectStringDataGridViewColumn.ReadOnly = false;		
			//
			// uxTipDataGridViewColumn
			//
			this.uxTipDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxTipDataGridViewColumn.DataPropertyName = "Tip";
			this.uxTipDataGridViewColumn.HeaderText = "Tip";
			this.uxTipDataGridViewColumn.Name = "uxTipDataGridViewColumn";
			this.uxTipDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxTipDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxTipDataGridViewColumn.ReadOnly = false;		
			//
			// uxKolumneDataGridViewColumn
			//
			this.uxKolumneDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxKolumneDataGridViewColumn.DataPropertyName = "Kolumne";
			this.uxKolumneDataGridViewColumn.HeaderText = "Kolumne";
			this.uxKolumneDataGridViewColumn.Name = "uxKolumneDataGridViewColumn";
			this.uxKolumneDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxKolumneDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxKolumneDataGridViewColumn.ReadOnly = false;		
			//
			// uxFilterDataGridViewColumn
			//
			this.uxFilterDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxFilterDataGridViewColumn.DataPropertyName = "Filter";
			this.uxFilterDataGridViewColumn.HeaderText = "Filter";
			this.uxFilterDataGridViewColumn.Name = "uxFilterDataGridViewColumn";
			this.uxFilterDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxFilterDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxFilterDataGridViewColumn.ReadOnly = false;		
			//
			// uxFilterAdvDataGridViewColumn
			//
			this.uxFilterAdvDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxFilterAdvDataGridViewColumn.DataPropertyName = "FilterAdv";
			this.uxFilterAdvDataGridViewColumn.HeaderText = "FilterAdv";
			this.uxFilterAdvDataGridViewColumn.Name = "uxFilterAdvDataGridViewColumn";
			this.uxFilterAdvDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxFilterAdvDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxFilterAdvDataGridViewColumn.ReadOnly = false;		
			//
			// uxPoredakDataGridViewColumn
			//
			this.uxPoredakDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxPoredakDataGridViewColumn.DataPropertyName = "Poredak";
			this.uxPoredakDataGridViewColumn.HeaderText = "Poredak";
			this.uxPoredakDataGridViewColumn.Name = "uxPoredakDataGridViewColumn";
			this.uxPoredakDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxPoredakDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxPoredakDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxReportDataGridView);
			this.Name = "ReportDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxReportErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxReportDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxReportBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				ReportDataGridViewEventArgs args = new ReportDataGridViewEventArgs();
				args.Report = _currentReport;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class ReportDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Report	_Report;
	
			/// <summary>
			/// the  Entities.Report instance.
			/// </summary>
			public Entities.Report Report
			{
				get { return _Report; }
				set { _Report = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxReportDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnReportDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxReportDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnReportDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxReportDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = ReportList[e.RowIndex].ID;
						break;
					case "uxNazivDataGridViewColumn":
						e.Value = ReportList[e.RowIndex].Naziv;
						break;
					case "uxIDParentDataGridViewColumn":
						e.Value = ReportList[e.RowIndex].IDParent;
						break;
					case "uxSelectStringDataGridViewColumn":
						e.Value = ReportList[e.RowIndex].SelectString;
						break;
					case "uxTipDataGridViewColumn":
						e.Value = ReportList[e.RowIndex].Tip;
						break;
					case "uxKolumneDataGridViewColumn":
						e.Value = ReportList[e.RowIndex].Kolumne;
						break;
					case "uxFilterDataGridViewColumn":
						e.Value = ReportList[e.RowIndex].Filter;
						break;
					case "uxFilterAdvDataGridViewColumn":
						e.Value = ReportList[e.RowIndex].FilterAdv;
						break;
					case "uxPoredakDataGridViewColumn":
						e.Value = ReportList[e.RowIndex].Poredak;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxReportDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnReportDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxReportDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						ReportList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxNazivDataGridViewColumn":
						ReportList[e.RowIndex].Naziv = (System.String)e.Value;
						break;
					case "uxIDParentDataGridViewColumn":
						ReportList[e.RowIndex].IDParent = (System.Int32?)e.Value;
						break;
					case "uxSelectStringDataGridViewColumn":
						ReportList[e.RowIndex].SelectString = (System.String)e.Value;
						break;
					case "uxTipDataGridViewColumn":
						ReportList[e.RowIndex].Tip = (System.Int32)e.Value;
						break;
					case "uxKolumneDataGridViewColumn":
						ReportList[e.RowIndex].Kolumne = (System.String)e.Value;
						break;
					case "uxFilterDataGridViewColumn":
						ReportList[e.RowIndex].Filter = (System.Byte[])e.Value;
						break;
					case "uxFilterAdvDataGridViewColumn":
						ReportList[e.RowIndex].FilterAdv = (System.Byte[])e.Value;
						break;
					case "uxPoredakDataGridViewColumn":
						ReportList[e.RowIndex].Poredak = (System.String)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
